/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenRewriteStream;
import org.antlr.runtime.tree.CommonTree;
/**
 *
 * @author daniel
 */
public class Ast {
	static String inputFileName ;
	static String outputFilename;
	static BufferedWriter out ;

	/**
	 * Param --help -> show options
	 * 		  filePath - read input from file specified by path 
	 * 		 -out filePath - write output to file specified by path 
	 * @param args the command line arguments
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		if (args.length == 0 ){
			System.out.println("Privide input file : ");
			outputFilename = "out.txt";
		}
		if (args[0].trim().equals("--help")){
			System.out.println("-f filePath - read input from file specified by path ");
			System.out.println("-out filePath - write output to file specified by path ");
			return;
		}

		if (args[0].trim().equals("-out")){
			// output file was pecified
			if (args.length > 2){
				outputFilename = args[1];
				//input file was specified
				if (args.length > 3){
					inputFileName = args[2];
				}
				else{
					System.out.println("Privide input file : ");
					InputStreamReader converter = new InputStreamReader(System.in);
					BufferedReader inn = new BufferedReader(converter);
					inputFileName = inn.readLine();
					inn.close();
				}
			}
			else {
				System.out.println("Invalid option, please privide output file when -out option is used!");
				return;
			}
		}
		else {
			inputFileName = args[0];
			if (args.length>1){
				if (args[1].trim().equals("-out")){
					if (args.length > 2)
						outputFilename = args[1];
					else{
						System.out.println("Invalid option, please privide output file when -out option is used!");
						return;
					}		
				}
				else {
					System.out.println("Invalid option, please use --help option to see parameters");
					return;
				}
			}
			else{
				outputFilename= "out.txt";
			}
		}
		try{
			out  = new BufferedWriter(new FileWriter(outputFilename));
			ANTLRFileStream f = new ANTLRFileStream(inputFileName);
			$grammarLexer lex = new $grammarLexer(f);
			TokenRewriteStream tokens = new TokenRewriteStream(lex);

			$grammarParser p = new $grammarParser(tokens);
			CommonTree tree = (CommonTree)p.$functionName().getTree();
			printTree(tree);
			out.flush();
			out.close();
		}catch (IOException e){
			System.out.println("Please input an valid file to parse!");
		} catch (RecognitionException e) {
			System.out.println("Unable to parse input file!\n"+e.getMessage());
		}
		finally{
			try {
				out.close();
			} catch (IOException e) {
			}
		}
	}
	public static void printTree(CommonTree t) throws IOException {
		if ( t != null ) {
			if (t.getText()!=null)
			out.write(t.getText() + ((t.getChildCount()>0)?"(":"") + " ");
			for ( int i = 0; i < t.getChildCount(); i++ ) {
				printTree((CommonTree)t.getChild(i));
				if (i!=t.getChildCount()-1)
					out.write(",");
			}
			if (t.getText()!=null)
			out.write(((t.getChildCount()>0)?")":""));
		}
	}
}
